﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace WinUI {
    public class PostgreSqlTable {
        public PostgreSqlTable(string xmlFile) {
            XmlTextReader reader = new XmlTextReader(xmlFile);
            reader.WhitespaceHandling = WhitespaceHandling.None;
            XmlDocument xmlDoc = new XmlDocument();
            //Load the file into the XmlDocument

            xmlDoc.Load(reader);
            //Close off the connection to the file.

            reader.Close();
            //Add and item representing the document to the listbox

            //XmlDocument xml = new XmlDocument();
            //xml.LoadXml(xmlFile);

            {
                string xPathName = "report/header/title";
                XmlNode nodeName = xmlDoc.SelectSingleNode(xPathName);
                _name = nodeName.InnerText;
            }

            string xPathField = "report/section/table/rows";
            XmlNode nodeRows = xmlDoc.SelectSingleNode(xPathField);

            foreach (XmlNode xn in nodeRows.ChildNodes) {
                PostgreSqlField field = new PostgreSqlField(xn);
                _fields.Add(field);

                if (field.PrimaryKey) {
                    _primaryKeys.Add(field);
                }

                // insert fields
                if (field.DataType != PostgreSqlDataType.Serial
                    && field.Name != "created" && field.Name != "updated" && field.Name != "updator"
                    && field.Name != "deleted" && field.Name != "deletion_time") {
                    _insertFields.Add(field);
                }

                // update fields
                if (field.Name != "created" && field.Name != "creator" && field.Name != "updated") {
                    _updateFields.Add(field);
                }

                // input fields
                if (field.DataType != PostgreSqlDataType.Serial
                    && field.Name != "created" && field.Name != "creator"
                    && field.Name != "updated" && field.Name != "updator"
                    && field.Name != "deleted" && field.Name != "deletion_time") {
                    _inputFields.Add(field);

                    if (!field.NotNull) {
                        _requiredFields.Add(field);
                    }

                    if (field.DataType == PostgreSqlDataType.Int) {
                        _intFields.Add(field);
                    } else if (field.DataType == PostgreSqlDataType.Numeric) {
                        _doubleFields.Add(field);
                    } else if (field.DataType == PostgreSqlDataType.TimeStamp) {
                        _dateTimeFields.Add(field);
                    }
                }

            }
        }

        private string _name;
        public string Name {
            get { return _name; }
            set { _name = value; }
        }

        private List<PostgreSqlField> _primaryKeys = new List<PostgreSqlField>();

        public List<PostgreSqlField> PrimaryKeys {
            get { return _primaryKeys; }
            set { _primaryKeys = value; }
        }

        private List<PostgreSqlField> _fields = new List<PostgreSqlField>();
        public List<PostgreSqlField> Fields {
            get { return _fields; }
            set { _fields = value; }
        }

        private List<PostgreSqlField> _insertFields = new List<PostgreSqlField>();
        public List<PostgreSqlField> InsertFields {
            get { return _insertFields; }
            set { _insertFields = value; }
        }

        private List<PostgreSqlField> _updateFields = new List<PostgreSqlField>();
        public List<PostgreSqlField> UpdateFields {
            get { return _updateFields; }
            set { _updateFields = value; }
        }

        public string JavaName {
            get {
                return CodeGenerationUtil.GenJavaName(_name);
            }
        }

        public string DisplayName {
            get {
                // TODO
                return CodeGenerationUtil.GenDisplayName(_name);
            }
        }

        public string JavaNameLowerCase {
            get {
                return CodeGenerationUtil.GenJavaNameLowerCase(_name);
            }
        }

        private List<PostgreSqlField> _inputFields = new List<PostgreSqlField>();
        public List<PostgreSqlField> InputFields {
            get { return _inputFields; }
            set { _inputFields = value; }
        }

        private List<PostgreSqlField> _requiredFields = new List<PostgreSqlField>();
        public List<PostgreSqlField> RequiredFields {
            get { return _requiredFields; }
            set { _requiredFields = value; }
        }

        private List<PostgreSqlField> _intFields = new List<PostgreSqlField>();
        public List<PostgreSqlField> IntFields {
            get { return _intFields; }
            set { _intFields = value; }
        }

        private List<PostgreSqlField> _doubleFields = new List<PostgreSqlField>();
        public List<PostgreSqlField> DoubleFields {
            get { return _doubleFields; }
            set { _doubleFields = value; }
        }

        private List<PostgreSqlField> _dateTimeFields = new List<PostgreSqlField>();
        public List<PostgreSqlField> DateTimeFields {
            get { return _dateTimeFields; }
            set { _dateTimeFields = value; }
        }

    }
}
